package com.fourmob.datetimepicker;

import ohos.accessibility.ability.AccessibilityInfo;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.Component;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.Calendar;

/**
 * 日历换算工具类
 * @since 2021-04-12
 */
public class Utils {
    public static final int PULSE_ANIMATOR_DURATION = 544;
    private static HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x000110, "DatePickerDialog");
    public static int getDaysInMonth(int month, int year) {
        switch (month) {
            case Calendar.JANUARY:
            case Calendar.MARCH:
            case Calendar.MAY:
            case Calendar.JULY:
            case Calendar.AUGUST:
            case Calendar.OCTOBER:
            case Calendar.DECEMBER:
                return 31;
            case Calendar.APRIL:
            case Calendar.JUNE:
            case Calendar.SEPTEMBER:
            case Calendar.NOVEMBER:
                return 30;
            case Calendar.FEBRUARY:
                return (year % 4 == 0) ? 29 : 28;
            default:
                throw new IllegalArgumentException("Invalid Month");
        }
    }

    public static String getSpellInMonth(int month) {
        switch (month) {
            case Calendar.JANUARY: return "1月";
            case Calendar.MARCH: return "3月";
            case Calendar.MAY: return "5月";
            case Calendar.JULY: return "7月";
            case Calendar.AUGUST: return "8月";
            case Calendar.OCTOBER: return "10月";
            case Calendar.DECEMBER: return "12月";
            case Calendar.APRIL: return "4月";
            case Calendar.JUNE: return "6月";
            case Calendar.SEPTEMBER: return "9月";
            case Calendar.NOVEMBER: return "11月";
            case Calendar.FEBRUARY: return "2月";
            default:
                throw new IllegalArgumentException("Invalid Month");
        }
    }

    public static AnimatorValue getPulseAnimator(Component labelToAnimate, float decreaseRatio, float increaseRatio) {
        AnimatorValue animatorProperty = new AnimatorValue();
        animatorProperty.setCurveType(5);
        animatorProperty.setLoopedCount(1);

        AnimatorValue.ValueUpdateListener valueUpdateListener = new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
              HiLog.info(label, "onUpdateListener " + v);

            }
        };
        animatorProperty.setValueUpdateListener(valueUpdateListener);
        animatorProperty.setDuration(PULSE_ANIMATOR_DURATION);
        return animatorProperty;
    }

    public static AnimatorProperty getPulsePropertyAnimator(Component labelToAnimate,
                                                            float decreaseRatio, float increaseRatio) {
        AnimatorProperty animatorProperty = new AnimatorProperty();
        animatorProperty.scaleYFrom(decreaseRatio);
        animatorProperty.scaleYBy(increaseRatio);
        animatorProperty.scaleXFrom(decreaseRatio);
        animatorProperty.scaleXBy(increaseRatio);
        animatorProperty.setTarget(labelToAnimate);
        animatorProperty.setStateChangedListener(new Animator.StateChangedListener() {
            @Override
            public void onStart(Animator animator) {
                HiLog.info(label,"Animator onStart");
            }

            @Override
            public void onStop(Animator animator) {
                HiLog.info(label,"Animator onStop");
            }

            @Override
            public void onCancel(Animator animator) {
                HiLog.info(label,"Animator onCancel");
            }

            @Override
            public void onEnd(Animator animator) {
                HiLog.info(label,"Animator onEnd");
            }

            @Override
            public void onPause(Animator animator) {
                HiLog.info(label,"Animator onPause");
            }

            @Override
            public void onResume(Animator animator) {
                HiLog.info(label,"Animator onResume");
            }
        });
        animatorProperty.setDelay(PULSE_ANIMATOR_DURATION);
        return animatorProperty;
    }

    protected static float scaleHs(float v) {
        float res = 0;
        if (v <= (float) 1 / 3)
            res = (float) (-1.2 * v + 1);
        else if (v > (float) 1 / 3 && v <= (float) 2 / 3)
            res = (float) (-0.3 * v + 0.7);
        else if (v > (float) 2 / 3)
            res = (float) (1.5 * v - 0.5);
        return res;
    }

    public static boolean isJellybeanOrLater() {
        return true;
    }

    /**
     * Try to speak the specified text, for accessibility. Only available on JB or later.
     * @param text Text to announce.
     */
    /*@SuppressLint("NewApi")*/
    public static void tryAccessibilityAnnounce(Component view, CharSequence text) {
        if (isJellybeanOrLater() && view != null && text != null) {
       /*     view.announceForAccessibility(text);*/
            HiLog.info(label, "DPD_tryAccessibilityAnnounce");
            view.announceAccessibility(text + "");

            HiLog.info(label, "DPD_tryAccessibilityAnnounce01");
        }
    }

    public static boolean isTouchExplorationEnabled(AccessibilityInfo accessibilityManager) {
      return accessibilityManager.isClickable();
    }

}
